{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(2.8963869, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "aa = tf.exp(2.0)\n",
    "bb = tf.add(aa, 1.)\n",
    "cc = tf.sqrt(bb)\n",
    "print(cc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Exp:0\", shape=(), dtype=float32)\n",
      "Tensor(\"Add:0\", shape=(), dtype=float32)\n",
      "Tensor(\"Sqrt:0\", shape=(), dtype=float32)\n",
      "tf.Tensor(2.8963869, shape=(), dtype=float32)\n",
      "tf.Tensor(1.9282846, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "@tf.function\n",
    "def func(inp):\n",
    "    aa = tf.exp(inp)\n",
    "    print(aa)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    print(bb)\n",
    "    cc = tf.sqrt(bb)\n",
    "    print(cc)\n",
    "    return cc\n",
    "print(func(tf.constant(2.0, dtype=tf.float32)))\n",
    "print(func(tf.constant(1.0, dtype=tf.float32)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(7.389056, shape=(), dtype=float32)\n",
      "tf.Tensor(8.389056, shape=(), dtype=float32)\n",
      "tf.Tensor(2.8963869, shape=(), dtype=float32)\n",
      "tf.Tensor(2.8963869, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "def func(inp):\n",
    "    aa = tf.exp(inp)\n",
    "    print(aa)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    print(bb)\n",
    "    cc = tf.sqrt(bb)\n",
    "    print(cc)\n",
    "    return cc\n",
    "print(func(2.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tf.Graph().as_default():\n",
    "    aa = tf.exp(2.0)\n",
    "    print(aa)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    print(bb)\n",
    "    cc = tf.sqrt(bb)\n",
    "    print(cc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Take Gradient of a Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(1.2755646, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "#take gradient of a variable\n",
    "def func(w):\n",
    "    aa = tf.exp(w)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    cc = tf.sqrt(bb)\n",
    "    return cc\n",
    "\n",
    "w = tf.Variable(2.0, dtype=tf.float32)\n",
    "\n",
    "with tf.GradientTape() as tape:\n",
    "    y = func(w)\n",
    "print(tape.gradient(y, w))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gradient Tape watch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(11923.831, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# take gradient of a non-variable\n",
    "def func(inp):\n",
    "    aa = tf.exp(inp)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    cc = tf.sqrt(bb)\n",
    "    return cc\n",
    "\n",
    "with tf.GradientTape() as tape:\n",
    "    inp = tf.constant(2.0, dtype=tf.float32)\n",
    "    tape.watch(inp)\n",
    "    inp1 = inp*2\n",
    "    inp2 = inp1**2\n",
    "    out = func(inp2)\n",
    "    print(tape.gradient(out, inp1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0> <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# @tf.function\n",
    "def func():\n",
    "    aa = tf.Variable(1.0, dtype=tf.float32)\n",
    "    bb = tf.Variable(2.0, dtype=tf.float32)\n",
    "    print(aa, bb)\n",
    "print(func())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>\n",
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.0>\n",
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=5.0>\n",
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=4.0>\n"
     ]
    }
   ],
   "source": [
    "aa = tf.Variable(2.0)\n",
    "print(aa)\n",
    "aa.assign(3.0)\n",
    "print(aa)\n",
    "aa.assign_add(2.0)\n",
    "print(aa)\n",
    "aa.assign_sub(1.0)\n",
    "print(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@tf.function\n",
    "def func(inp):\n",
    "    aa = tf.exp(inp)\n",
    "    bb = tf.add(aa, 1.)\n",
    "    cc = tf.sqrt(bb)\n",
    "    return cc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 3, 4, 'hello']\n",
      "[[1, 2, 3, 4], [1, 3, 4, 'hello'], 34]\n",
      "4\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "aa = [1, 2, 3, 4]\n",
    "bb = [1, 3, 4, 'hello']\n",
    "cc = [aa, bb, 34]\n",
    "print(aa)\n",
    "print(bb)\n",
    "print(cc)\n",
    "\n",
    "print(len(aa))\n",
    "print(len(bb))\n",
    "print(len(cc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 3, 4, 'hello']\n",
      "[[1, 2, 3, 4], [1, 3, 4, 'hello'], 34]\n",
      "34\n",
      "[[1, 2, 3, 4], [1, 3, 4, 'hello'], 'there']\n"
     ]
    }
   ],
   "source": [
    "aa = [1, 2, 3, 4]\n",
    "bb = [1, 3, 4, 'hello']\n",
    "cc = [aa, bb, 34]\n",
    "print(aa)\n",
    "print(bb)\n",
    "print(cc)\n",
    "print(cc[2]) # print cc 中的最后一个元素\n",
    "cc[2]='there'\n",
    "print(cc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[3, 4, 5]\n",
      "[1.2, 2.2, 3.2, 4.2, 5.2, 6.2]\n"
     ]
    }
   ],
   "source": [
    "aa = [1, 2, 3, 4, 5, 6]\n",
    "bb = [o+0.2 for o in aa]\n",
    "print(aa)\n",
    "print(aa[2:5]) #slice 第二号到第四号元素，也是一个list\n",
    "print(bb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, how are you doing\n",
      "ell\n",
      "True\n",
      "False\n",
      "7\n",
      "asdf iouon\n"
     ]
    }
   ],
   "source": [
    "aa = 'hello, how are you doing'\n",
    "print(aa)\n",
    "print(aa[1:4])\n",
    "\n",
    "print('how' in aa)\n",
    "print('hodw' in aa)\n",
    "\n",
    "print(aa.find('how'))\n",
    "print(' '.join(['asdf', 'iouon']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key': 'value', 2: 'there', 'key2': 'hello'}\n",
      "value\n",
      "there\n"
     ]
    }
   ],
   "source": [
    "aa = {'key': 'value', 2: 'there', 'key2':'hello'}\n",
    "print(aa)\n",
    "print(aa['key'])\n",
    "print(aa[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 4, 5)\n",
      "(1, 2, 4)\n"
     ]
    }
   ],
   "source": [
    "aa = (1, 2, 4, 5)\n",
    "print(aa)\n",
    "print(aa[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"how\" is in the string\n",
      "====================\n",
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      ",\n",
      " \n",
      "h\n",
      "o\n",
      "w\n"
     ]
    }
   ],
   "source": [
    "aa = 'hello, how'\n",
    "s = 'how'\n",
    "if s in aa:\n",
    "    print('\"%s\" is in the string'%s)\n",
    "else:\n",
    "    print('\"%s\" not in the string'%s)\n",
    "    \n",
    "print('=='*10)\n",
    "for i in aa:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(aa):\n",
    "    print(f'hello, this is func, argument is {aa}')\n",
    "    ret = aa +'word'\n",
    "    return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, this is func, argument is hello \n",
      "hello word\n"
     ]
    }
   ],
   "source": [
    "ret = func('hello ')\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "----------\n",
      "7\n",
      "9\n",
      "11\n",
      "13\n",
      "15\n",
      "----------\n",
      "0.2\n"
     ]
    }
   ],
   "source": [
    "class B:\n",
    "    def __init__(self):\n",
    "        self.bvar = 0.2\n",
    "\n",
    "class A(B):\n",
    "    def __init__(self):\n",
    "        super(A, self).__init__()\n",
    "        self.var = 0\n",
    "        \n",
    "    def func(self):\n",
    "        self.var +=1\n",
    "    def __call__(self):\n",
    "        self.var+=2\n",
    "\n",
    "aa = A()\n",
    "for i in range(5):\n",
    "    aa.func()\n",
    "    print(aa.var)\n",
    "print('--'*5)\n",
    "for i in range(5):\n",
    "    aa()\n",
    "    print(aa.var)\n",
    "print('--'*5)\n",
    "print(aa.bvar)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "==========\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "aa = range(10)\n",
    "print(aa)\n",
    "print('=='*5)\n",
    "print([i for i in range(10)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.30046073 0.22540455 0.35071192 0.08742732]\n",
      " [0.32245581 0.48066222 0.94634912 0.9820427 ]\n",
      " [0.19283751 0.8807215  0.92611392 0.62253314]\n",
      " [0.73793292 0.1877469  0.7334378  0.77869074]]\n",
      "--------------------\n",
      "[[0.30046073 0.32245581 0.19283751 0.73793292]\n",
      " [0.22540455 0.48066222 0.8807215  0.1877469 ]\n",
      " [0.35071192 0.94634912 0.92611392 0.7334378 ]\n",
      " [0.08742732 0.9820427  0.62253314 0.77869074]]\n",
      "--------------------\n",
      "[[-1.76312223 -3.32082472  1.60157797  3.10559922]\n",
      " [-5.19538803 -4.73282826  4.40146347  3.03329846]\n",
      " [ 9.11153887  6.29619103 -4.64981382 -5.24606558]\n",
      " [-5.65855334 -1.64217284  1.80062625  2.55100826]]\n",
      "--------------------\n",
      "[[ 1.00000000e+00 -2.40017707e-17  1.27019883e-16 -2.91570978e-16]\n",
      " [-3.98476466e-16  1.00000000e+00 -4.69247668e-17 -3.37207919e-16]\n",
      " [-9.90276656e-17 -2.92502341e-16  1.00000000e+00  5.18605237e-17]\n",
      " [ 1.23260543e-16  4.33118071e-16  2.50008338e-16  1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.random.rand(4, 4)\n",
    "print(arr1)\n",
    "print('--'*10)\n",
    "print(np.transpose(arr1))\n",
    "print('--'*10)\n",
    "inv_arr1 = np.linalg.inv(arr1)\n",
    "print(inv_arr1)\n",
    "print('--'*10)\n",
    "print(np.dot(arr1, inv_arr1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "==========\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "==========\n",
      "[[[ 0  2  4  6]\n",
      "  [ 8 10 12 14]\n",
      "  [16 18 20 22]]\n",
      "\n",
      " [[12 14 16 18]\n",
      "  [20 22 24 26]\n",
      "  [28 30 32 34]]]\n"
     ]
    }
   ],
   "source": [
    "#假设A矩阵的shape是[2, 3, 4]\n",
    "#   B矩阵的shape是[3, 4]\n",
    "#假设要做按位加法操作那么B会被自动广播成[1，3，4]进而变成[2, 3, 4]，然后就可以操作了\n",
    "A = np.arange(24).reshape(2, 3, 4)\n",
    "print(A)\n",
    "print('=='*5)\n",
    "B = np.arange(12).reshape(3, 4)\n",
    "print(B)\n",
    "print('=='*5)\n",
    "print(A+B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=8, shape=(5, 5), dtype=bool, numpy=\n",
       "array([[ True, False, False, False, False],\n",
       "       [ True,  True,  True, False, False],\n",
       "       [ True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True,  True]])>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.sequence_mask([1, 3, 5, 6, 7], maxlen=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
